package co.fitstart.admin.web.controller.user.distributor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.service.DistributionRecordService;
import co.fitstart.admin.service.DistributorService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.user.distributor.DistributionRecord;
import co.fitstart.entity.user.distributor.Distributor;
import co.fitstart.entity.user.distributor.DistributorUserRelation;

@Controller
@RequestMapping("/distributors/{distributorId}")
@MenuSetting("menu-user")
public class DistributorViewController {
    
private static final Logger logger = LoggerFactory.getLogger(DistributorViewController.class);
    
    @Autowired
    private DistributorService distributorService;
    
    @Autowired
    private DistributionRecordService distributionRecordService;
    
    /**
     * 
     * @param distributorId
     * @return
     */
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public ModelAndView agentOrder(@PathVariable("distributorId") Long distributorId) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distributor-order-view");
        
        Distributor distributor = distributorService.load(distributorId);
        modelAndView.addObject("distributor", distributor);
        
        List<DistributorUserRelation> relations = distributorService.listRelation(distributor);
        modelAndView.addObject("relations", relations);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param agentId
     * @return
     */
    @RequestMapping(value = "/distribution-records", method = RequestMethod.GET)
    public ModelAndView withdrawal(@PathVariable("distributorId") Long distributorId, Page page, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distribution-record-list");
        
        Distributor distributor = distributorService.load(distributorId);
        modelAndView.addObject("distributor", distributor);
        
        List<DistributionRecord> records = distributionRecordService.listByDistributor(distributor, page);
        modelAndView.addObject("records", records);
        
        
        modelAndView.addObject("page", page);
        
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param distributorId
     * @return
     */
    @RequestMapping(value = "/edit-info", method = RequestMethod.GET)
    public ModelAndView editInfo(@PathVariable("distributorId") Long distributorId) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distributor-info-edit");
        
        Distributor distributor = distributorService.load(distributorId);
        modelAndView.addObject("distributor", distributor);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param distributorId
     * @return
     */
    @RequestMapping(value = "/edit-info", method = RequestMethod.PATCH)
    public @ResponseBody String onEditInfo(@Preloading("distributorId") Distributor distributor) {
        
        Result result = new Result();
        
        try {
            
            distributorService.editDistributor(distributor);
            
            result.success().message("编辑信息成功");
            
        } catch (Exception e) {
            
            logger.error("onEditInfo() - ", e);
            
            result.fail().message("编辑信息失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param distributionRecordId
     * @param distributorId
     * @return
     */
    @RequestMapping(value = "/distribution-records/{distributionRecordId}/pay", method = RequestMethod.GET)
    public ModelAndView pay(@PathVariable("distributionRecordId") Long distributionRecordId,
                            @PathVariable("distributorId") Long distributorId) {
        ModelAndView modelAndView = new ModelAndView("user/distributor/distribution-record-pay");
        
        modelAndView.addObject("distributorId", distributorId);
        modelAndView.addObject("distributionRecordId", distributionRecordId);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param distributorId
     * @param agentRecordId
     * @return
     */
    @RequestMapping(value = "/distribution-records/{distributionRecordId}/pay", method = RequestMethod.PATCH)
    public @ResponseBody String onPay(@PathVariable("distributorId") Long distributorId,
                                      @PathVariable("distributionRecordId") Long distributionRecordId) {
        Result result = new Result();
        
        try {
            
            Distributor distributor = distributorService.load(distributorId);
            
            DistributionRecord distributionRecord = distributionRecordService.load(distributionRecordId);
            
            distributorService.payDistribution(distributor, distributionRecord);
            
            result.success().message("代言人打款成功");
            
        } catch (Exception e) {
            
            logger.error("onPay() - ", e);
            result.fail().message("代言人打款失败");
        }
        
        return JsonUtils.toJsonString(result);
    }

}
